package mao.thinkinjava.io;

import java.io.File;
import java.io.FilenameFilter;
import java.util.regex.Pattern;

public class TestFile {
    public static void main(String[] args) {
        File file = new File("C:\\Users\\xk1008\\IdeaProjects\\untitled\\src\\com\\mao");
        String[] list = file.list(new MyFilenameFilter(".*think.*"));
        for (int i = 0; i < list.length; i++) {
            System.out.println(list[i]);
        }

        String[] list1 = file.list();
        for (int i = 0; i < list1.length; i++) {
            System.out.println(list1[i]);
        }
    }

    public static class MyFilenameFilter implements FilenameFilter{
        private Pattern pattern;
        public MyFilenameFilter(String reg){
            pattern = Pattern.compile(reg);
        }
        @Override  //传入的dir有什么用呢？
        //file.list()会对于每个下面文件调用accept()方法。
        public boolean accept(File dir, String name) {
            System.out.println(dir);
            return pattern.matcher(name).matches();
        }
    }
}
